package com.beilang.hotline.telrecord.dao;

import com.beilang.base.core.dao.PaginationBaseDAO;
import com.beilang.base.pagination.PaginationHelper;
import com.beilang.base.util.Constant;
import com.beilang.base.util.DateUtil;
import com.beilang.base.util.StringUtil;
import com.beilang.hotline.telrecord.model.TelrecordModel;
import com.beilang.hotline.telrecord.to.SearchTO;
import com.beilang.hotline.user.model.UserModel;
import com.beilang.hotline.person.model.PersonModel;

import java.util.*;

@SuppressWarnings("unchecked")
public class TelrecordDAO extends PaginationBaseDAO {

    public TelrecordModel createTelrecordModel(TelrecordModel telrecordModel) {
        return (TelrecordModel) this.save(telrecordModel);
    }

    public TelrecordModel saveTelrecordModel(TelrecordModel telrecordModel) {
        return (TelrecordModel) this.save(telrecordModel);
    }

    public void deleteTelrecordModel(Long recordId) {
        this.remove(TelrecordModel.class, recordId);
    }

    public TelrecordModel getTelrecordModel(Long recordId) {
        return (TelrecordModel) this.get(TelrecordModel.class, recordId);
    }

    public Integer searchCount(UserModel user, SearchTO searchTO) {
        Map map = new HashMap();
        map = setCompanyTypeMap(map, user.getAccessCompanyList());
        if (StringUtil.isEmpty(searchTO.getQuickSearchIndicator())) {
            map = setcCommonMap(map ,searchTO);
        } else {
            map = setMap(map,user, searchTO);
        }
        return (Integer) getSqlMapClientTemplate().queryForObject("searchTelrecordCount", map);
    }

    public void deleteDraftTelrecord(UserModel user) {
        Map map;
        if (user != null && !StringUtil.isEmpty(user.getUserId())) {
            map = new HashMap();
            map.put("userId", user.getUserId());
            Date dateTo = DateUtil.add(DateUtil.getDate(), Calendar.DATE, -1);
            map.put("dateTo", dateTo);
            getSqlMapClientTemplate().delete("deleteDraftTelrecord", map);
        }
    }

    public List searchList(UserModel user, PaginationHelper helper, SearchTO searchTO) {
        Map map=new HashMap();
        map = setCompanyTypeMap(map, user.getAccessCompanyList());
        if (StringUtil.isEmpty(searchTO.getQuickSearchIndicator())) {
            map = setcCommonMap(map,searchTO);
        } else {
            map = setMap(map,user, searchTO);
        }
        map = constructPaginationMap(helper, map);
        return getSqlMapClientTemplate().queryForList("searchTelrecordList", map);
    }

    protected String mapSortField(String dataField) {
        if (StringUtil.isNullOrBlank(dataField)) return dataField;
        if (dataField.equalsIgnoreCase("recordId")) return "record_id";
        if (dataField.equalsIgnoreCase("titleDesc")) return "title_id";
        if (dataField.equalsIgnoreCase("recordTimeStr")) return "record_time";
        if (dataField.equalsIgnoreCase("companyNameDesc")) return "company_id";
        if (dataField.equalsIgnoreCase("titleRemarks")) return "title_remarks";
        if (dataField.equalsIgnoreCase("pname")) return "name";
        if (dataField.equalsIgnoreCase("curOwnerNameDesc")) return "current_owner";
        if (dataField.equalsIgnoreCase("recordStatusDesc")) return "record_id";
        if (dataField.equalsIgnoreCase("creatorNameDesc")) return "create_id";
        if (dataField.equalsIgnoreCase("sysNameDesc")) return "system_id";
        return dataField;
    }

    private Map setcCommonMap(Map map,SearchTO searchTO) {
        map.put("recordId", searchTO.getRecordId());
        map.put("systemId", searchTO.getSystemId());
        map.put("moduleId", searchTO.getModuleId());
        map.put("issueId", searchTO.getIssueId());
        String titleRemarks = StringUtil.convertEmpty2Null(searchTO.getTitleRemarks());
        if (titleRemarks != null) titleRemarks = "%" + titleRemarks + "%";
        map.put("titleRemarks", titleRemarks);
        if (!StringUtil.isEmpty(searchTO.getCurrentOwner())) {
            map.put("currentOwner", searchTO.getCurrentOwner());
        }
        if (!StringUtil.isEmpty(searchTO.getCreatorId())) {
            map.put("creatorId", searchTO.getCreatorId());
        }
        if (!StringUtil.isEmpty(searchTO.getRecordStatus())) {
            map.put("recordStatus", searchTO.getRecordStatus());
        }
        if (!StringUtil.isEmpty(searchTO.getRecordTimeStr())) {
            Date dateFrom = DateUtil.parse(searchTO.getRecordTimeStr());
            Date dateTo = DateUtil.add(dateFrom, Calendar.DATE, 1);
            map.put("dateFrom", dateFrom);
            map.put("dateTo", dateTo);
        }
        if (!StringUtil.isEmpty(searchTO.getRecordFromTimeStr())) {
            Date dateFrom = DateUtil.parse(searchTO.getRecordFromTimeStr());
            map.put("dateFrom1", dateFrom);
        }
        if (!StringUtil.isEmpty(searchTO.getRecordToTimeStr())) {
            Date dateTo = DateUtil.parse(searchTO.getRecordToTimeStr());
            Date dateTo1 = DateUtil.add(dateTo, Calendar.DATE, 1);
            map.put("dateTo1", dateTo1);
        }
        map.put("companyId", searchTO.getCompanyId());
        return map;
    }

    private Map setMap(Map map,UserModel user, SearchTO searchTO) {
        if (!StringUtil.isNullOrBlank(searchTO.getQuickSearchIndicator())) {
            map.put("quickSearchInd", searchTO.getQuickSearchIndicator());
            //quick search
            if (searchTO.getQuickSearchIndicator().equalsIgnoreCase("2")) {
                //等待我处理
                map.put("userId", user.getUserId());
                map.put("recordStatus", Constant.STATUS_INPROGRESS);
            } else if (searchTO.getQuickSearchIndicator().equalsIgnoreCase("3")) {
                //我创建，未关闭
                map.put("userId", user.getUserId());
                map.put("recordStatus", Constant.STATUS_INPROGRESS);
            } else if (searchTO.getQuickSearchIndicator().equalsIgnoreCase("4")) {
                //我创建，所有
                map.put("userId", user.getUserId());
            } else if (searchTO.getQuickSearchIndicator().equalsIgnoreCase("5")) {
                //我处理过，未关闭
                map.put("userId", user.getUserId());
                map.put("recordStatus", Constant.STATUS_INPROGRESS);
            } else if (searchTO.getQuickSearchIndicator().equalsIgnoreCase("6")) {
                //我处理过，未关闭
                map.put("userId", user.getUserId());
            } else if (searchTO.getQuickSearchIndicator().equalsIgnoreCase("8")) {
                //所有未关闭
                map.put("recordStatus", Constant.STATUS_INPROGRESS);
            } else if (searchTO.getQuickSearchIndicator().equalsIgnoreCase("9")) {
                //所有关闭
                map.put("recordStatus", Constant.STATUS_COMPLETE);
            } else if (searchTO.getQuickSearchIndicator().equalsIgnoreCase("10")) {
                //所有
            } else if (searchTO.getQuickSearchIndicator().equalsIgnoreCase("12")) {
                //本周创建
                Date dateFrom = DateUtil.getStartDayOfWeek(DateUtil.getDate());
                Date dateTo = DateUtil.add(DateUtil.getEndDayOfWeek(DateUtil.getDate()), Calendar.DATE, 1);
                map.put("dateFrom", dateFrom);
                map.put("dateTo", dateTo);
            } else if (searchTO.getQuickSearchIndicator().equalsIgnoreCase("13")) {
                //本周关闭
                map.put("recordStatus", Constant.STATUS_COMPLETE);
                Date dateFrom = DateUtil.getStartDayOfWeek(DateUtil.getDate());
                Date dateTo = DateUtil.add(DateUtil.getEndDayOfWeek(DateUtil.getDate()), Calendar.DATE, 1);
                map.put("dateFrom", dateFrom);
                map.put("dateTo", dateTo);
            } else if (searchTO.getQuickSearchIndicator().equalsIgnoreCase("14")) {
                //上周创建
                Date dateFrom = DateUtil.getStartDayOfWeek(DateUtil.getDate(), -1);
                Date dateTo = DateUtil.add(DateUtil.getEndDayOfWeek(DateUtil.getDate(), -1), Calendar.DATE, 1);
                map.put("dateFrom", dateFrom);
                map.put("dateTo", dateTo);
            } else if (searchTO.getQuickSearchIndicator().equalsIgnoreCase("15")) {
                //上周关闭
                map.put("recordStatus", Constant.STATUS_COMPLETE);
                Date dateFrom = DateUtil.getStartDayOfWeek(DateUtil.getDate(), -1);
                Date dateTo = DateUtil.add(DateUtil.getEndDayOfWeek(DateUtil.getDate(), -1), Calendar.DATE, 1);
                map.put("dateFrom", dateFrom);
                map.put("dateTo", dateTo);
            } else if (searchTO.getQuickSearchIndicator().equalsIgnoreCase("16")) {
                //本月创建
                Date dateFrom = DateUtil.getStartDayOfMonth(DateUtil.getDate());
                Date dateTo = DateUtil.add(DateUtil.getEndDayOfMonth(DateUtil.getDate()), Calendar.DATE, 1);
                map.put("dateFrom", dateFrom);
                map.put("dateTo", dateTo);
            } else if (searchTO.getQuickSearchIndicator().equalsIgnoreCase("17")) {
                //本月关闭
                map.put("recordStatus", Constant.STATUS_COMPLETE);
                Date dateFrom = DateUtil.getStartDayOfMonth(DateUtil.getDate());
                Date dateTo = DateUtil.add(DateUtil.getEndDayOfMonth(DateUtil.getDate()), Calendar.DATE, 1);
                map.put("dateFrom", dateFrom);
                map.put("dateTo", dateTo);
            } else if (searchTO.getQuickSearchIndicator().equalsIgnoreCase("18")) {
                //上月创建
                Date dateFrom = DateUtil.getStartDayOfMonth(DateUtil.getDate(), Calendar.MONTH, -1);
                Date dateTo = DateUtil.add(DateUtil.getEndDayOfMonth(DateUtil.getDate(), Calendar.MONTH, -1), Calendar.DATE, 1);
                map.put("dateFrom", dateFrom);
                map.put("dateTo", dateTo);
            } else if (searchTO.getQuickSearchIndicator().equalsIgnoreCase("19")) {
                //上月关闭
                map.put("recordStatus", Constant.STATUS_COMPLETE);
                Date dateFrom = DateUtil.getStartDayOfMonth(DateUtil.getDate(), Calendar.MONTH, -1);
                Date dateTo = DateUtil.add(DateUtil.getEndDayOfMonth(DateUtil.getDate(), Calendar.MONTH, -1), Calendar.DATE, 1);
                map.put("dateFrom", dateFrom);
                map.put("dateTo", dateTo);
            } else if (searchTO.getQuickSearchIndicator().equalsIgnoreCase("20")) {
                //一周内未处理
                Date dateFrom = DateUtil.getStartDayOfWeek(DateUtil.getDate());
                Date dateTo = DateUtil.add(DateUtil.getEndDayOfWeek(DateUtil.getDate()), Calendar.DATE, 1);
                map.put("dateFrom", dateFrom);
                map.put("dateTo", dateTo);
            }

        }
        return map;
    }

    public List searchReportList(UserModel user, SearchTO searchTO) {
        Map map = new HashMap();
        map = setCompanyTypeMap(map, user.getAccessCompanyList());
        if (StringUtil.isEmpty(searchTO.getQuickSearchIndicator())) {
            map = setcCommonMap(map,searchTO);
        } else {
            map = setMap(map,user, searchTO);
        }
        return getSqlMapClientTemplate().queryForList("searchTelrecordRptList", map);
    }


    private Map setCompanyTypeMap(Map map, List companyTypeList) {
        if (companyTypeList != null && companyTypeList.size() > 0) {
            StringBuffer sysWhereClause = new StringBuffer("(");
            for (Object aList : companyTypeList) {
                String s = (String) aList;
                if (!StringUtil.isNullOrBlank(s)) {
                    if (!sysWhereClause.toString().equalsIgnoreCase("(")) {
                        sysWhereClause.append(" or ");
                    }
                    sysWhereClause.append("(");
                    sysWhereClause = sysWhereClause.append("company_type = '");
                    sysWhereClause = sysWhereClause.append(s);
                    sysWhereClause = sysWhereClause.append("' or ");
                    sysWhereClause = sysWhereClause.append("company_type like '");
                    sysWhereClause = sysWhereClause.append(s).append("|").append("%'");
                    sysWhereClause = sysWhereClause.append(" or ");
                    sysWhereClause = sysWhereClause.append("company_type like '%");
                    sysWhereClause = sysWhereClause.append("|").append(s).append("|%'");
                    sysWhereClause = sysWhereClause.append(" or ");
                    sysWhereClause = sysWhereClause.append("company_type like '%");
                    sysWhereClause = sysWhereClause.append("|").append(s).append("'");
                    sysWhereClause = sysWhereClause.append(")");
                }
            }
            String companyType = sysWhereClause.toString();
            companyType = companyType + ")";
            if (!"()".equalsIgnoreCase(companyType)) {
                map.put("companyType", companyType);
            }
        }
        return map;
    }

    public List getSearchCompanyList(String companyName, List companyTypeList) {
        Map map = new HashMap();
        map = setCompanyTypeMap(map, companyTypeList);
        if (!StringUtil.isEmpty(StringUtil.trim(companyName))) {
            map.put("companyName", "%" + companyName + "%");
        }

        return getSqlMapClientTemplate().queryForList("searchCompanyListFilter", map);
    }

    public PersonModel getCompanyPersonByTelephone(String telephonenumber)
    {
        Map map = new HashMap();
        if (!StringUtil.isEmpty(telephonenumber)) {
            map.put("telephonenumber", "%" + telephonenumber + "%");
        }
        List list =  getSqlMapClientTemplate().queryForList("getCompanyPersonByTelephone", map);
        if (list!=null&&!list.isEmpty())
        {
            return (PersonModel)list.get(0);
        }
        return null;
    }
}
